using System;
using System.Data;
using System.Web;
using digit.Core;

namespace SMS.UserManager
{
    public class MembersManager
    {
        #region CONSTRUCTORS
        static MembersManager()
        {
            _data = new MembersData(Util.GetConnectionString("SMSSERVICE"));
            _error = "";
        }
        #endregion

        #region ATTRIBUTES
        private static MembersData _data;
        private static string _error;
        #endregion

        #region PROPERTIES
        public static string ErrorMessage
        {
            get
            {
                return _error;
            }
        }
        #endregion

        #region METHODS

        public static bool CheckEmail(string strEmail)
        {
            _error = "";
            try
            {
                return _data.CheckEmail(strEmail);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static string CheckUserNameByMail(string Email)
        {
            _error = "";
            try
            {
                DataTable dTable = _data.CheckUserNameByMail(Email);
                if (dTable.Rows.Count < 1) return "";

                MembersInfo info = new MembersInfo(dTable.Rows[0]);

                return info.UserName;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return "";
            }
        }

        public static MembersInfo CheckUserName(string UserName)
        {
            _error = "";
            try
            {
                DataTable dTable = _data.Select(UserName);
                if (dTable.Rows.Count < 1) return null;

                MembersInfo info = new MembersInfo(dTable.Rows[0]);

                HttpContext.Current.Session["Member"] = DBNull.Value;
                HttpContext.Current.Session["Member"] = info;

                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool ResetPass(string strEmail, string strPass)
        {
            _error = "";
            try
            {
                object[] values = new object[2];
                values[0] = strEmail;
                values[1] = strPass;

                return _data.ResetPass(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static DataTable SelectTopNewMember(int top)
        {
            _error = "";
            try
            {
                object[] values = { top };
                return _data.SelectTopNewMember(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool Login(string username, string password)
        {
            object[] values = { username, password };
            DataTable dTable = _data.Login(values);

            if (dTable.Rows.Count < 1)
            {
                return false;
            }
            else
            {
                MembersInfo objMemberinfo = new MembersInfo(dTable.Rows[0]);

                HttpContext.Current.Session["Member"] = DBNull.Value;
                HttpContext.Current.Session["Member"] = objMemberinfo;

                return true;
            }
        }

        public static bool UserAuthen(string username, string password)
        {
            object[] values = { username, password };
            DataTable dTable = _data.Login(values);

            if (dTable.Rows.Count < 1) return false;
            else return true;
        }

        public static bool Insert(string fullName, string userName, string password, string pin, string identity, string email, string mobile, int amount, byte type)
        {
            _error = "";
            try
            {
                object[] values = new object[10];
                values[0] = DBNull.Value;
                values[1] = fullName;
                values[2] = userName;
                values[3] = password;
                values[4] = pin;
                values[5] = identity;
                values[6] = email;
                values[7] = mobile;
                values[8] = amount;
                values[9] = type;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Insert(MembersInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[10];
                values[0] = info.FullName;
                values[1] = info.UserName;
                values[2] = info.Password;
                values[3] = info.PIN;
                values[4] = info.IdentityNo;
                values[5] = info.Email;
                values[6] = info.MobileNo;
                values[7] = info.Amount;
                values[8] = info.VerifyCode;
                values[9] = info.Type;

                return _data.Insert(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(int memberID, string fullName, string userName, string password, string pin, string identity, string email, string mobile, int amount, DateTime createdDate, DateTime lastLogin, byte type, bool status)
        {
            _error = "";
            try
            {
                object[] values = new object[13];
                values[0] = memberID;
                values[1] = fullName;
                values[2] = userName;
                values[3] = password;
                values[4] = pin;
                values[5] = identity;
                values[6] = email;
                values[7] = mobile;
                values[8] = amount;
                values[9] = createdDate;
                values[10] = Util.ReplaceNullTo(lastLogin, DBNull.Value);
                values[11] = type;
                values[12] = status;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool ChangePass(string userName, string currentPass, string Pass)
        {
            _error = "";
            try
            {
                object[] values = new object[3];
                values[0] = userName;
                values[1] = currentPass;
                values[2] = Pass;
                return _data.ChangePass(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Update(MembersInfo info)
        {
            _error = "";
            try
            {
                object[] values = new object[14];
                values[0] = info.MemberID;
                values[1] = info.FullName;
                values[2] = info.UserName;
                values[3] = info.Password;
                values[4] = info.PIN;
                values[5] = info.IdentityNo;
                values[6] = info.Email;
                values[7] = info.MobileNo;
                values[8] = info.Amount;
                values[9] = info.VerifyCode;
                values[10] = info.CreatedDate;
                values[11] = Util.ReplaceNullTo(info.LastLogin, DBNull.Value);
                values[12] = info.Type;
                values[13] = info.Status.HasValue ? (object)info.Status : (object)DBNull.Value;

                return _data.Update(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(int memberID)
        {
            _error = "";
            try
            {
                object[] values = { memberID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static bool Delete(MembersInfo info)
        {
            _error = "";
            try
            {
                object[] values = { info.MemberID };
                return _data.Delete(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return false;
            }
        }

        public static MembersInfo Select(int memberID)
        {
            _error = "";
            try
            {
                object[] values = { memberID };
                DataTable dTable = _data.Select(values);
                if (dTable.Rows.Count < 1) return null;


                MembersInfo info = new MembersInfo(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static MembersInfo Select(string UserName)
        {
            _error = "";
            try
            {
                DataTable dTable = _data.Select(UserName);
                if (dTable.Rows.Count < 1) return null;


                MembersInfo info = new MembersInfo(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataSet ListAll(int Status, int PageIndex, int PageSize, string UserName)
        {
            _error = "";
            try
            {
                object[] values = { Status, PageIndex, PageSize, UserName };
                return _data.ListAll(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }


        public static DataTable Search(string UserName)
        {
            _error = "";
            try
            {
                object[] values = { UserName };
                return _data.Search(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static DataTable SelectAll()
        {
            _error = "";
            try
            {
                return _data.SelectAll();
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool VerifyPin(string userName, string userPin)
        {
            try
            {
                object[] values = { DBNull.Value, userName, userPin };
                return _data.VerifyPin(values);
            }
            catch (Exception ex)
            { return false; }
        }

        public static string VerifyCode(int memberID, string verifyCode)
        {
            _error = "";
            try
            {
                object[] values = { memberID, DBNull.Value, verifyCode };
                return _data.VerifyCode(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return "";
            }
        }

        public static int GetAccAmount(string userName)
        {
            try
            {
                object[] values = { null, Util.ReplaceDBNullTo(userName, Util.NULL_STRING) };
                return _data.GetAccAmount(values);
            }
            catch
            {
                return 0;
            }
        }

        public static bool UpdatePIN(int MemberID, string PIN)
        {
            _error = "";
            try
            {
                object[] values = new object[2];
                values[0] = MemberID;
                values[1] = PIN;

                return _data.UpdatePIN(values);
            }
            catch
            { return false; }
        }

        public static MembersInfo SelectByMobileNo(string MobileNo)
        {
            _error = "";
            try
            {
                DataTable dTable = _data.SelectByMobileNo(MobileNo);
                if (dTable.Rows.Count < 1) return null;
                MembersInfo info = new MembersInfo(dTable.Rows[0]);
                return info;
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }

        public static bool ResetPIN(int MemberID, string Email, string MobileNo, string NewPINCode)
        {
            _error = "";
            try
            {
                object[] values = new object[4];
                values[0] = Util.ReplaceDBNullTo(MemberID, Util.NULL_LONG);
                values[1] = Util.ReplaceDBNullTo(Email, Util.NULL_STRING);
                values[2] = Util.ReplaceDBNullTo(MobileNo, Util.NULL_STRING);
                values[3] = Util.ReplaceDBNullTo(NewPINCode, Util.NULL_STRING);

                return _data.ResetPIN(values);
            }
            catch
            { return false; }
        }

        #endregion
    }
}